{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "<Element 'model' at 0x109d51170>\n",
      "model\n",
      "outgoingPath: 1 --- 2\n",
      "outgoingPath: 11 --- 12\n",
      "outgoingPath: 17 --- 3\n",
      "outgoingPath: 2 --- 17\n",
      "outgoingPath: 4 --- 13\n",
      "outgoingPath: 4 --- 5\n",
      "outgoingPath: 5 --- 6\n",
      "outgoingPath: 6 --- 7\n",
      "outgoingPath: 7 --- 11\n",
      "outgoingPath: 7 --- 8\n",
      "outgoingPath: 9 --- 10\n",
      "outgoingPath: 9 --- 4\n",
      "path: 1 --- 2, sourcePoint: 1, destPoint: 2, length: 500, routingCost: 1, maxVel: 500, revVel: 500, locked: false\n",
      "path: 11 --- 12, sourcePoint: 11, destPoint: 12, length: 1700, routingCost: 1, maxVel: 1000, revVel: 1000, locked: false\n",
      "path: 17 --- 3, sourcePoint: 17, destPoint: 3, length: 480, routingCost: 1, maxVel: 1000, revVel: 1000, locked: false\n",
      "path: 2 --- 17, sourcePoint: 2, destPoint: 17, length: 2259, routingCost: 1, maxVel: 1000, revVel: 0, locked: false\n",
      "path: 4 --- 13, sourcePoint: 4, destPoint: 13, length: 1300, routingCost: 1, maxVel: 1000, revVel: 1000, locked: false\n",
      "path: 4 --- 5, sourcePoint: 4, destPoint: 5, length: 2900, routingCost: 1, maxVel: 500, revVel: 500, locked: false\n",
      "name: dir, value: horizon\n",
      "path: 5 --- 6, sourcePoint: 5, destPoint: 6, length: 2425, routingCost: 1, maxVel: 500, revVel: 500, locked: false\n",
      "name: dir, value: vertical\n",
      "path: 6 --- 7, sourcePoint: 6, destPoint: 7, length: 2500, routingCost: 1, maxVel: 500, revVel: 500, locked: false\n",
      "name: dir, value: horizon\n",
      "path: 7 --- 11, sourcePoint: 7, destPoint: 11, length: 2500, routingCost: 1, maxVel: 1000, revVel: 1000, locked: false\n",
      "path: 7 --- 8, sourcePoint: 7, destPoint: 8, length: 2550, routingCost: 1, maxVel: 500, revVel: 500, locked: false\n",
      "name: dir, value: vertical\n",
      "path: 9 --- 10, sourcePoint: 9, destPoint: 10, length: 7511, routingCost: 1, maxVel: 1000, revVel: 1, locked: false\n",
      "path: 9 --- 4, sourcePoint: 9, destPoint: 4, length: 2250, routingCost: 1, maxVel: 500, revVel: 500, locked: false\n",
      "name: dir, value: horizon\n",
      "vehicle name: Vehicle-01, length: 1000, energyLevelCritical: 30\n",
      "vehicle name: Vehicle-02, length: 1000, energyLevelCritical: 30\n",
      "Response HTTP Status Code: 200\n",
      "Response HTTP Response Body: b'[ {\\n  \"name\" : \"Vehicle-01\",\\n  \"properties\" : {\\n    \"port\" : \"502\",\\n    \"tcs:preferredAdapterClass\" : \"com.lvsrobot.vehicle.ExampleCommAdapterFactory\",\\n    \"ip\" : \"192.168.0.10\"\\n  },\\n  \"length\" : 1000,\\n  \"energyLevelGood\" : 90,\\n  \"energyLevelCritical\" : 30,\\n  \"energyLevel\" : 95,\\n  \"integrationLevel\" : \"TO_BE_UTILIZED\",\\n  \"procState\" : \"PROCESSING_ORDER\",\\n  \"transportOrder\" : \"TOrder-01EFTPMHDYAG8T9X427H2SX5E9\",\\n  \"currentPosition\" : \"8\",\\n  \"state\" : \"EXECUTING\"\\n}, {\\n  \"name\" : \"Vehicle-02\",\\n  \"properties\" : {\\n    \"tcs:preferredAdapterClass\" : \"org.opentcs.virtualvehicle.LoopbackCommunicationAdapterFactory\"\\n  },\\n  \"length\" : 1000,\\n  \"energyLevelGood\" : 90,\\n  \"energyLevelCritical\" : 30,\\n  \"energyLevel\" : 100,\\n  \"integrationLevel\" : \"TO_BE_RESPECTED\",\\n  \"procState\" : \"UNAVAILABLE\",\\n  \"transportOrder\" : null,\\n  \"currentPosition\" : null,\\n  \"state\" : \"IDLE\"\\n} ]'\n",
      "<class 'list'>\n",
      "Response HTTP Status Code: 200\n",
      "Response HTTP Response Body: b''\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "try:\n",
    "    import xml.etree.cElementTree as ET\n",
    "except ImportError:\n",
    "    import xml.etree.ElementTree as ET\n",
    "tree = ET.ElementTree(file=\"data/model.xml\")    \n",
    "print(tree.getroot())\n",
    "\n",
    "\n",
    "import xml.dom.minidom\n",
    "\n",
    "dom = xml.dom.minidom.parse(\"data/model.xml\")\n",
    "root = dom.documentElement\n",
    "print(root.nodeName)\n",
    "points = root.getElementsByTagName(\"point\")\n",
    "point_dic = {}\n",
    "path_dic = {}\n",
    "\n",
    "for point in points:\n",
    "    # list_point_name = list(map(int, po))\n",
    "    point_dic.update({point.getAttribute(\"name\"): [point.getAttribute(\"xPosition\"), point.getAttribute(\"yPosition\"), point.getAttribute(\"vehicleOrientationAngle\"), point.getAttribute(\"type\")]})\n",
    "    # print('point: {}, x:{}, y: {}, angle: {}, type: {}'.format(point.getAttribute(\"name\"), point.getAttribute(\"xPosition\"), point.getAttribute(\"yPosition\"), point.getAttribute(\"vehicleOrientationAngle\"), point.getAttribute(\"type\")))\n",
    "    paths = point.getElementsByTagName(\"outgoingPath\")\n",
    "    out_path_list = []\n",
    "    for path in paths:\n",
    "        out_path_list.append(path.getAttribute(\"name\"))\n",
    "        print('outgoingPath: {}'.format(path.getAttribute(\"name\")))\n",
    "    point_dic[point.getAttribute(\"name\")].append(out_path_list)\n",
    "        \n",
    "paths = root.getElementsByTagName(\"path\")\n",
    "\n",
    "for path in paths:\n",
    "    path_dic.update({path.getAttribute(\"name\"): [path.getAttribute(\"sourcePoint\"), path.getAttribute(\"destinationPoint\"), path.getAttribute(\"length\"), path.getAttribute(\"routingCost\"), path.getAttribute(\"maxVelocity\"), path.getAttribute(\"maxReverseVelocity\"), path.getAttribute(\"locked\")]})\n",
    "    print(\"path: {}, sourcePoint: {}, destPoint: {}, length: {}, routingCost: {}, maxVel: {}, revVel: {}, locked: {}\".format(path.getAttribute(\"name\"), path.getAttribute(\"sourcePoint\"), path.getAttribute(\"destinationPoint\"), path.getAttribute(\"length\"), path.getAttribute(\"routingCost\"), path.getAttribute(\"maxVelocity\"), path.getAttribute(\"maxReverseVelocity\"), path.getAttribute(\"locked\")))\n",
    "    propertys = path.getElementsByTagName(\"property\")\n",
    "    propertys_list = []\n",
    "    for property in propertys:\n",
    "        # propertys_list.append()\n",
    "        print(\"name: {}, value: {}\".format(property.getAttribute(\"name\"), property.getAttribute(\"value\")))\n",
    "vehicles = root.getElementsByTagName(\"vehicle\")\n",
    "\n",
    "for vehicle in vehicles:\n",
    "    print(\"vehicle name: {}, length: {}, energyLevelCritical: {}\".format(vehicle.getAttribute(\"name\"), vehicle.getAttribute(\"length\"), vehicle.getAttribute(\"energyLevelCritical\")))\n",
    "    \n",
    "import requests\n",
    "import json\n",
    "\n",
    "def send_request():\n",
    "    # Restreves Vehicle\n",
    "    # GET http://localhost:55200/v1/vehicles\n",
    "    \n",
    "    try:\n",
    "        response = requests.get(\n",
    "            url=\"http://localhost:55200/v1/vehicles\",\n",
    "        )\n",
    "        print('Response HTTP Status Code: {status_code}'.format(\n",
    "            status_code=response.status_code))\n",
    "        print('Response HTTP Response Body: {content}'.format(\n",
    "            content=response.content))\n",
    "    except requests.exceptions.RequestException:\n",
    "        print('HTTP Request failed')  \n",
    "    return  json.loads(response.content)   \n",
    "response = send_request()        \n",
    "print(type(response)) \n",
    "\n",
    "\n",
    "def send_order():\n",
    "    # creat order\n",
    "    # POST http://localhost:55200/v1/transportOrders/Move-238\n",
    "\n",
    "    try:\n",
    "        response = requests.post(\n",
    "            url=\"http://localhost:55200/v1/transportOrders/Move-239\",\n",
    "            headers={\n",
    "                \"Content-Type\": \"application/json; charset=utf-8\",\n",
    "            },\n",
    "            data=json.dumps({\n",
    "                \"destinations\": [\n",
    "                    {\n",
    "                        \"properties\": [\n",
    "                            {\n",
    "                                \"key\": \"key1\",\n",
    "                                \"value\": \"value1\"\n",
    "                            }\n",
    "                        ],\n",
    "                        \"locationName\": \"B\",\n",
    "                        \"operation\": \"NOP\"\n",
    "                    }\n",
    "                ]\n",
    "            })\n",
    "        )\n",
    "        print('Response HTTP Status Code: {status_code}'.format(\n",
    "            status_code=response.status_code))\n",
    "        print('Response HTTP Response Body: {content}'.format(\n",
    "            content=response.content))\n",
    "    except requests.exceptions.RequestException:\n",
    "        print('HTTP Request failed')\n",
    "send_order()        \n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}